$(function(){
    $.get('http://localhost:3000/product',(data)=>{
        data.forEach(item=>{
             let html=`
        <tr class="rowData">
            <td>${item.id}</td>
            <td>${item.title}</td>
            <td>${item.zaiyao}</td>
            <td>${item.body}</td>
            <td>${item.type}</td>
            <td>${item.author}</td>
            <td>
            <input type="button" value="编辑" onclick="edit(${item.id})">
            <input type="button" value="删除" onclick="del(${item.id})">
            </td>
        </tr>
        `
        $('#tbData').append(html)
        })
       
    })
})

//查找
//查找
function query() {
    let keyword = $('#keyword').val();
    getJsonResult().then(data => {
        if (keyword) {
            let arr = data.filter(item => {
                return item.id == keyword || item.title == keyword || item.zaiyao == keyword || item.body == keyword 
                || item.type == keyword || item.author==keyword
            })
            return renderFn(arr)
        }
       renderFn(data)
    })
}

function getJsonResult() {
    return new Promise(function (resolve, reject) {
        $.get('http://localhost:3000/product', data => {
            resolve(data)
        })
    })
}

function renderFn(arr) {
    let row = $('.rowData');
    let tb = $('#tbData');
    row.remove();
    arr.forEach(item => {
        let html = `
        <tr class="rowData">
            <td>${item.id}</td>
            <td>${item.title}</td>
            <td>${item.zaiyao}</td>
            <td>${item.body}</td>
            <td>${item.type}</td>
            <td>${item.author}</td>
            <td>
            <input type="button" value="编辑" onclick="edit(${item.id})">
            <input type="button" value="删除" onclick="del(${item.id})">
            </td>
        </tr>
        `
        tb.append(html)
    })
}

//删除
function del(id){
    delProductById(id).then(data=>{
        console.log(data);
    })
}

function delProductById(id){
    return new Promise(function(resolve,reject){
        $.ajax({
            url:`http://localhost:3000/product/${id}`,
            type:'delete',
            success:data=>{
                resolve(data);
                location.href='./index.html'
            }
        })
    })
}

//新增
function add(obj){
    location.href='./addOrEdit.html',
    postProduct(obj).then(data=>{
        console.log(data);
    })
}

function postProduct(obj){
    return new Promise(function(resolve,reject){
        $.post('http://localhost:3000/product',obj,data=>{
            resolve(data)
        })
    })
}

//保存事件
function save(){
   let obj={
       title:$('[name=title]').val(),
       zaiyao:$('[name=author]').val(),
       body:$('[name=body]').val(),
       type:$('[name=type]').val(),
       author:$('[name=author]').val(),
   }
   postProduct(obj).then(data=>{
       if(data.code==1000){
           location.href='./index.html'
       }else{
           alert(data.msg)
       }
   })
}

//取消事件
function cancel(){
    location.href='./index.html'
}
